package com.sk.bilibili.view.fragment.member;

import android.content.Intent;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.sk.bilibili.adapter.vlayout.main.home.HeaderAdapter;
import com.sk.bilibili.adapter.vlayout.member.home.CoinAdapter;
import com.sk.bilibili.adapter.vlayout.member.home.FavoritesAdapter;
import com.sk.bilibili.adapter.vlayout.member.home.FollowThemAdapter;
import com.sk.bilibili.adapter.vlayout.member.home.PraiseAdapter;
import com.sk.bilibili.adapter.vlayout.member.home.SeeAdapter;
import com.sk.bilibili.bean.Coin;
import com.sk.bilibili.bean.Favorites;
import com.sk.bilibili.bean.FollowThem;
import com.sk.bilibili.bean.Praise;
import com.sk.bilibili.bean.VideoSet;
import com.sk.bilibili.callBack.FavoritesClickCallBack;
import com.sk.bilibili.callBack.HeaderClickCallBack;
import com.sk.bilibili.callBack.ThemClickCallBack;
import com.sk.bilibili.callBack.VideoSetCallBack;
import com.sk.bilibili.event.MemberEvent;
import com.sk.bilibili.model.fragment.member.HomeModel;
import com.sk.bilibili.view.activity.CoinVideoSetActivity;
import com.sk.bilibili.view.activity.CollectActivity;
import com.sk.bilibili.view.activity.PraiseVideoSetActivity;
import com.sk.bilibili.view.activity.ThemActivity;
import com.sk.bilibili.view.activity.VideoSetActivity;
import com.sk.bilibili.view.fragment.BaseListFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class HomeFragment extends BaseListFragment<HomeModel> implements HeaderClickCallBack,
        FavoritesClickCallBack, ThemClickCallBack {

    private static final int VIDEO = 2;
    private static final int FAVORITES = 3;
    private static final int FOLLOW_THEM = 4;
    private static final int COIN = 0;
    private static final int PRAISE = 1;
    private SeeAdapter videoAdapter;
    private FavoritesAdapter favoritesAdapter;
    private FollowThemAdapter followThemAdapter;
    private CoinAdapter coinAdapter;
    private PraiseAdapter praiseAdapter;
    private Integer memberId;

    public HomeFragment(Integer memberId) {
        this.memberId = memberId;
    }

    @Override
    protected void afterSetView() {
        super.afterSetView();
        model.loadData(memberId);
    }

    @Override
    protected void initAdapter(DelegateAdapter adapter) {
        HeaderAdapter headerAdapter = new HeaderAdapter(getContext(), "视频");
        headerAdapter.setCallBack(this, VIDEO);
        adapter.addAdapter(headerAdapter);
        videoAdapter = new SeeAdapter(getContext(), this);
        adapter.addAdapter(videoAdapter);
        headerAdapter = new HeaderAdapter(getContext(), "收藏");
        headerAdapter.setCallBack(this, FAVORITES);
        adapter.addAdapter(headerAdapter);
        favoritesAdapter = new FavoritesAdapter(getContext(), this);
        adapter.addAdapter(favoritesAdapter);
        headerAdapter = new HeaderAdapter(getContext(), "追番");
        headerAdapter.setCallBack(this, FOLLOW_THEM);
        adapter.addAdapter(headerAdapter);
        followThemAdapter = new FollowThemAdapter(getContext(), this);
        adapter.addAdapter(followThemAdapter);
        headerAdapter = new HeaderAdapter(getContext(), "最近投币");
        headerAdapter.setCallBack(this, COIN);
        adapter.addAdapter(headerAdapter);
        coinAdapter = new CoinAdapter(getContext(), this);
        adapter.addAdapter(coinAdapter);
        headerAdapter = new HeaderAdapter(getContext(), "最近点赞");
        headerAdapter.setCallBack(this, PRAISE);
        adapter.addAdapter(headerAdapter);
        praiseAdapter = new PraiseAdapter(getContext(), this);
        adapter.addAdapter(praiseAdapter);
    }

    @Override
    public void onHeaderClick(Integer type) {
        switch (type) {
            case COIN:
                CoinVideoSetActivity.start(getContext(),memberId);
                break;
            case PRAISE:
                PraiseVideoSetActivity.start(getContext(),memberId);
                break;
            default:
                EventBus.getDefault().post(MemberEvent.switchEvent(type));
                break;
        }
    }

    @Override
    public void onFavoritesClick(Integer id) {
        CollectActivity.start(getContext(),id);
    }

    @Override
    public void onThemClick(Integer themId, int selection) {
        ThemActivity.start(getContext(), themId,selection);
    }

    public void loadVideoSuccess(List<VideoSet> records) {
        videoAdapter.setData(records);
    }

    public void loadFavoritesSuccess(List<Favorites> favorites) {
        favoritesAdapter.setData(favorites);
    }

    public void loadFollowThemSuccess(List<FollowThem> records) {
        followThemAdapter.setData(records);
    }

    public void loadCoinSuccess(List<Coin> records) {
        coinAdapter.setData(records);
    }

    public void loadPraiseSuccess(List<Praise> records) {
        praiseAdapter.setData(records);
    }

    public void onClickVideoSet(Integer id) {
        VideoSetActivity.start(getContext(), id);
    }

}